#!/usr/bin/env python
""" Parser for Google Finance

Parses Google Finance for information about mutual funds.
"""

from HTMLParser import HTMLParser
from urllib2 import urlopen

class GFParser(HTMLParser):
  googleFinanceURL = "http://www.google.com/finance?q="
  
  def __init__(self, symbol):
    HTMLParser.__init__(self)
    req = urlopen(self.googleFinanceURL+symbol)
    self.feed(req.read())
    req.close()
        
  def reset(self):
    HTMLParser.reset(self)
    self.tdData = []
  
  def handle_data(self, data):
    if len(data)>1 and 'td' in self.get_starttag_text():
      self.tdData.append(data)

if __name__ == '__main__':
  data = Spider('http://www.google.com/finance?q=JSVAX')

  for i in xrange(len(data.tdData)): 
    if 'Stocks' in data.tdData[i]: 
      print "Stocks = %s" % data.tdData[i+1].strip()
    if 'Bonds' in data.tdData[i]: 
      print "Bonds = %s " % data.tdData[i+1].strip()
